import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/Home';
import Classify from "../views/classify/Classify";
import Publish from "../views/publish/Publish";
import Profile from "../views/profile/Profile";
import Login from "../components/login/Login";
import Register from "../components/login/Register";
import Article from "../views/article/Article";
import PersonalCenter from "../views/profile/PersonalCenter";
import Search from "../views/search/Search";
Vue.use(VueRouter)
export default new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: "/home",
      component: Home,
    },
    {
      path: '/classify',
      component: Classify
    },
    {
      path: '/publish',
      component: Publish,
      beforeEnter: (to, from, next) => {
        // ...
        if (sessionStorage.getItem('username')){
          next();
        }else {
          alert('你还未登录,登录后才能发布！');
          next({
            path: '/login'
          })
        }
        next();
      }
    },
    {
      path: '/profile',
      component: Profile
    },
    {
      name: 'login',
      path: '/login',
      component: Login
    },
    {
      name: 'register',
      path: '/register',
      component: Register
    },
    {
      name: 'article',
      path: '/article',
      component: Article
    },
    {
      name: 'personal',
      path: '/personal',
      component: PersonalCenter,
      beforeEnter: (to, from, next) => {
        // ...
        if (sessionStorage.getItem('username')){
          next();
        }else {
          alert('请先登录！！！');
          next({
            path: '/login'
          })
        }
        next();
      }
    },
    {
      name: 'search',
      path: '/search',
      component: Search
    }
  ],
  mode: "history"
})
//解决路由重复点击带来的报错问题
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};
